package config

import (
	"fmt"

	"gitee.com/bjf-fhe/apinx/errors"
	"gitee.com/bjf-fhe/apinx/openapi"
	"github.com/getkin/kin-openapi/openapi3"
)

type Servers []*openapi.ServerSelection

func (s *Servers) CheckByDef(def *openapi3.T) error {
	if len(*s) == 0 {
		return errors.New(errors.ERROR_NO_SERVER_SELECTED)
	}
	for _, selection := range *s {
		if selection.Url != "" {
			selection.AddDef(&openapi3.Server{URL: selection.Url})
		} else if selection.No > -1 && selection.No < len(def.Servers) {
			selection.AddDef(def.Servers[selection.No])
		} else {
			return errors.New(errors.ERR_NO_SUCH_SERVER, fmt.Sprintf("%d", selection.No))
		}
	}
	return nil
}

//SelectOneServer 将第一个ServerSelection转换为openapi3.Server，其中的参数会被替换为传入的值
func (s *Servers) SelectOneServer() *openapi3.Server {
	return (*s)[0].ToStatic()
}
